﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Prototype
{
    class IniFile
    {
        private string INIFILE = Application.StartupPath + "\\Resources\\preferences.ini";

        // Required DLL's
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        #region Read / Write
        // Write to Ini
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, INIFILE);
        }

        public string IniReadValue(string Section, string Key)
        {
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.INIFILE);
            return temp.ToString();

        }
        #endregion

        #region Check Ini + Default
        // Check if Ini File exists
        public void CheckIni()
        {
            if (!File.Exists(INIFILE))
            {
                // Open File Name
                FileInfo fi = new FileInfo(INIFILE);

                // Create File
                FileStream fs = fi.Create();

                // Write Default settings
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("[STARTUP]");
                sw.WriteLine("load_splash=true");

                // Close
                sw.Dispose();
                sw.Close();
                fs.Dispose();
                fs.Close();
            }
        }
        #endregion
    }
}
